package com.jeecg.demo.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.jwt.util.ResponseMessage;
import org.jeecgframework.jwt.util.Result;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Api(value = "WaterCarriage", description = "水运-用户同步", tags = "WaterCarriage")
@Controller
@RequestMapping("/WaterCarriage")
public class WaterCarriage {

    @Autowired
    private SystemService systemService;

    /**
     * 水运-用户同步
     */
    @RequestMapping(value = "/market")
    @ResponseBody
    @ApiOperation(value = "用户同步", produces = "application/json")
    public AjaxJson market( HttpServletRequest request) {
        String message = null;
        message = "同步失败";
        AjaxJson j = new AjaxJson();
        Map<String,Object> maps = new HashMap<>();
        ArrayList list = new ArrayList();
        String sql = " SELECT\n" +
                "\txm.FULLNAME AS \"companyName\",\n" +
                "\txm.LOGINNAME AS \"userName\",\n" +
                "\txm.PASSWORD AS \"password\",\n" +
                "\txm.MOBILE AS \"phone\"\n" +
                "FROM\n" +
                "\tXM_MARKET xm ";
        List<Map<String, Object>> listMap = systemService.findForJdbc(sql);
        try{
            for (Map<String, Object> map : listMap) {
                list.add(map);
            }
            maps.put("userInfos",list);
            maps.put("isAll", "Y");
            maps.put("isEdit", "N");
            message = "同步成功";
            RestTemplate template = new RestTemplate();
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity<Map<String, Object>> entity = new HttpEntity(maps, headers);
            String ret = template.postForObject("http://gateway.hncldc.com/golder/api/alterSynchronizationUser", entity, String.class);
            System.out.println(ret);
        }catch (Exception e){
            e.printStackTrace();
            message = "同步失败";
            throw new BusinessException(e.getMessage());
        }
        j.setMsg(message);
        return j;
    }
}
